ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
Miranda.c
Go to the documentation of this file.
00001 
00016 #include "derivative.h"
00017 #include "utils.h" /* For Parity */
00018 #include "Miranda.h"
00019 #include "DSPI.h"
00020 #include <limits.h> /* To set limits */
00021 #include <stdlib.h>
00022 /*
00023  ******************************************************************************
00024  * constants
00025  ******************************************************************************
00026  */
00027 
00028 const uint16_t cau16MirandaReset[] = 
00029 {
00030   /* Sequence below is needed to perform a soft-reset */
00031                 /* Protocol 0 */
00032   (MIRANDA_DEVCTL | (MIRANDA_WRITE_REGISTER  << CHAR_BIT)),(0x0080u),
00033   (MIRANDA_DEVCTL | (MIRANDA_WRITE_REGISTER  << CHAR_BIT)),(0x00C0u),
00034   (MIRANDA_DEVCTL | (MIRANDA_WRITE_REGISTER  << CHAR_BIT)),(0x0040u),
00035             /* Protocol 1 */
00036   ((MIRANDA_DEVCTL >> 4u) | ((MIRANDA_WRITE_REGISTER >> 1u) << CHAR_BIT)),(0x0080u),
00037   ((MIRANDA_DEVCTL >> 4u) | ((MIRANDA_WRITE_REGISTER >> 1u) << CHAR_BIT)),(0x00C0u),
00038   ((MIRANDA_DEVCTL >> 4u) | ((MIRANDA_WRITE_REGISTER >> 1u) << CHAR_BIT)),(0x0040u),  
00039         /* Protocol 2 */
00040   (MIRANDA_DEVCTL_P2 | (MIRANDA_WRITE_REGISTER  << CHAR_BIT)),(0x8000u),
00041   (MIRANDA_DEVCTL_P2 | (MIRANDA_WRITE_REGISTER  << CHAR_BIT)),(0xC000u),
00042   (MIRANDA_DEVCTL_P2 | (MIRANDA_WRITE_REGISTER  << CHAR_BIT)),(0x4000u)    
00043 };
00044   
00045 const uint16_t cau16MirandaReadAllRegisters[] = 
00046 {
00047            /* Protocol 0 */             
00048   (MIRANDA_SN_0     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00049   (MIRANDA_SN_1     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00050   (MIRANDA_ID_0     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00051   (MIRANDA_ID_1     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00052   (MIRANDA_DEVCFG_0 | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00053   (MIRANDA_DEVCFG_1 | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00054   (MIRANDA_CD_0C    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00055   (MIRANDA_CD_0E    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00056   (MIRANDA_CD_10    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00057   (MIRANDA_CD_12    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00058   (MIRANDA_CD_14    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00059   (MIRANDA_CD_16    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00060   (MIRANDA_CD_18    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00061   (MIRANDA_CD_1A    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00062   (MIRANDA_CD_1C    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00063   (MIRANDA_CD_1E    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00064   (MIRANDA_DEVSTAT  | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00065   (MIRANDA_DEVCTL   | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00066   (MIRANDA_TEST     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00067   (MIRANDA_RATE_X   | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),  
00068   (MIRANDA_ACC_Y    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00069   (MIRANDA_ACC_Z    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00070   (MIRANDA_TEMP     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),  
00071   (MIRANDA_COUNT    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00072            /* Protocol 1 */ 
00073   ((MIRANDA_SN_0 >> 4u)     | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00074   ((MIRANDA_SN_1 >> 4u)     | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00075   ((MIRANDA_ID_0 >> 4u)     | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00076   ((MIRANDA_ID_1 >> 4u)     | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00077   ((MIRANDA_DEVCFG_0 >> 4u) | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00078   ((MIRANDA_DEVCFG_1 >> 4u) | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00079   ((MIRANDA_CD_0C >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00080   ((MIRANDA_CD_0E >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00081   ((MIRANDA_CD_10 >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00082   ((MIRANDA_CD_12 >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00083   ((MIRANDA_CD_14 >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00084   ((MIRANDA_CD_16 >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00085   ((MIRANDA_CD_18 >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00086   ((MIRANDA_CD_1A >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00087   ((MIRANDA_CD_1C >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00088   ((MIRANDA_CD_1E >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR),
00089   ((MIRANDA_DEVSTAT >> 4u)  | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00090   ((MIRANDA_DEVCTL >> 4u)   | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00091   ((MIRANDA_TEST >> 4u)     | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00092   ((MIRANDA_RATE_X >> 4u)   | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR),  
00093   ((MIRANDA_ACC_Y >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR),
00094   ((MIRANDA_ACC_Z >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR), 
00095   ((MIRANDA_TEMP >> 4u)     | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR),  
00096   ((MIRANDA_COUNT >> 4u)    | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR),  
00097            /* Protocol 2 */
00098   (MIRANDA_BANK_P2      | (MIRANDA_WRITE_REGISTER << CHAR_BIT)),(CLEAR),  
00099   (MIRANDA_RATE_X_P2    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00100   (MIRANDA_ACC_Y_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00101   (MIRANDA_ACC_Z_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00102   (MIRANDA_TEMP_P2      | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00103   (MIRANDA_BANK_P2      | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00104   (MIRANDA_TEST_P2      | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00105   (MIRANDA_RESET_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00106   (MIRANDA_SENS_SYNC_P2 | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00107   (MIRANDA_DEVSTAT_P2   | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00108   (MIRANDA_DEVCTL_P2    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00109   (MIRANDA_COUNT_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00110   (MIRANDA_DEV_ID_P2    | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00111   (MIRANDA_BANK_P2      | (MIRANDA_WRITE_REGISTER << CHAR_BIT)),(0x0200u),
00112   (MIRANDA_SN_0_P2      | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00113   (MIRANDA_SN_1_P2      | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00114   (MIRANDA_ID_0_P2      | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00115   (MIRANDA_ID_1_P2      | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00116   (MIRANDA_DEVCFG_0_P2  | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00117   (MIRANDA_DEVCFG_1_P2  | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00118   (MIRANDA_CD_0C_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00119   (MIRANDA_CD_0E_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),  
00120   (MIRANDA_CD_10_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00121   (MIRANDA_CD_12_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00122   (MIRANDA_CD_14_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),  
00123   (MIRANDA_CD_16_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00124   (MIRANDA_CD_18_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00125   (MIRANDA_CD_1A_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR), 
00126   (MIRANDA_CD_1C_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),  
00127   (MIRANDA_CD_1E_P2     | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR)    
00128 };
00129 
00130 const uint16_t cau16MirandaPerformAccelReading[] = 
00131 {
00132            /* Protocol 0 */             
00133   (MIRANDA_RATE_X  | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00134   (MIRANDA_ACC_Y   | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00135   (MIRANDA_ACC_Z   | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00136            /* Protocol 1 */             
00137   ((MIRANDA_RATE_X >> 4u)  | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR),
00138   ((MIRANDA_ACC_Y >> 4u)   | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR),
00139   ((MIRANDA_ACC_Z >> 4u)   | ((MIRANDA_READ_REGISTER | 0x80u)  << CHAR_BIT)),(CLEAR),
00140        /* Protocol 2 */         
00141   (MIRANDA_RATE_X_P2  | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00142   (MIRANDA_ACC_Y_P2   | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR),
00143   (MIRANDA_ACC_Z_P2   | (MIRANDA_READ_REGISTER  << CHAR_BIT)),(CLEAR)
00144 };
00145 
00146 const uint16_t cau16MirandaPerformAccelReadingSPI[] = 
00147 {
00148        /* Protocol 1 only */            
00149   (MIRANDA_READ_ACCEL | MIRANDA_READ_X),(CLEAR),
00150   (MIRANDA_READ_ACCEL | MIRANDA_READ_Y),(CLEAR),
00151   (MIRANDA_READ_ACCEL | MIRANDA_READ_Z),(CLEAR)
00152 };
00153  
00154  /*
00155  ******************************************************************************
00156  * Globals
00157  ******************************************************************************
00158  */
00159 /*
00160  ******************************************************************************
00161  * u8fnMirandaReadRegister
00162  ******************************************************************************
00163  */
00164 uint8_t u8fnMirandaReadRegister(const uint8_t cu8DSPIInstance, \
00165                                 const uint8_t cu8ChipSelect,   \
00166                                 uint16_t u16Register,          \
00167                                 uint16_t* pu16DataRead,        \
00168                                 const uint8_t cu8Protocol)
00169 {
00170   uint8_t u8Status;
00171   uint8_t u8RegisterOperationProtocol;
00172   
00173   u8Status = CLEAR;
00174           
00175   if (PROTOCOL_1 == cu8Protocol)
00176   {
00177     u8RegisterOperationProtocol = MIRANDA_READ_REGISTER | 0x80u;
00178     u16Register = (u16Register >> 4u);
00179   }
00180   else if ((PROTOCOL_0 == cu8Protocol) || (PROTOCOL_2 == cu8Protocol))
00181   {
00182     u8RegisterOperationProtocol = MIRANDA_READ_REGISTER;
00183   }       
00184   else
00185   {
00186         u8Status = MIRANDA_ERROR_PROTOCOL;
00187   }
00188           
00189   if (CLEAR == u8Status)
00190   {        
00191      u8Status = u8fnMirandaRegisterOperation(u8RegisterOperationProtocol, cu8DSPIInstance,   \
00192                                              cu8ChipSelect, u16Register, CLEAR,              \
00193                                              pu16DataRead, cu8Protocol);
00194   }
00195   
00196   return (u8Status);
00197 }
00198 /*
00199  ******************************************************************************
00200  * u8fnMirandaWriteRegister
00201  ******************************************************************************
00202  */
00203 uint8_t u8fnMirandaWriteRegister(const uint8_t cu8DSPIInstance, \
00204                                  const uint8_t cu8ChipSelect,   \
00205                                  uint16_t u16Register,          \
00206                                  uint16_t u16ValueToWrite,      \
00207                                  uint16_t* pu16DataRead,        \
00208                                  const uint8_t cu8Protocol)
00209 {
00210   uint8_t u8Status;
00211   uint8_t u8RegisterOperationProtocol;
00212   
00213   u8Status = CLEAR;
00214           
00215   if (PROTOCOL_1 == cu8Protocol)
00216   {
00217     u8RegisterOperationProtocol = MIRANDA_WRITE_REGISTER >> 1;
00218     u16Register = (u16Register >> 4u);    
00219   }
00220   else if ((PROTOCOL_0 == cu8Protocol) || (PROTOCOL_2 == cu8Protocol))
00221   {        
00222         u8RegisterOperationProtocol = MIRANDA_WRITE_REGISTER;
00223   }       
00224   else
00225   {
00226         u8Status = MIRANDA_ERROR_PROTOCOL;
00227   }
00228            
00229   if (CLEAR == u8Status)
00230   {
00231      u8Status = u8fnMirandaRegisterOperation(u8RegisterOperationProtocol, cu8DSPIInstance,  \
00232                                              cu8ChipSelect, u16Register, u16ValueToWrite,   \
00233                                              pu16DataRead, cu8Protocol);
00234   }
00235   
00236   return (u8Status);  
00237 }
00238 /*
00239  ******************************************************************************
00240  * u8fnMirandaReset
00241  ******************************************************************************
00242  */
00243 uint8_t u8fnMirandaReset(const uint8_t cu8DSPIInstance, \
00244                          const uint8_t cu8ChipSelect,   \
00245                          const uint8_t cu8Protocol)
00246 {
00247   uint8_t u8Status;
00248   uint8_t u8Index;
00249   uint16_t au16Result[N_ELEMENTS(cau16MirandaReset)/3u];
00250   
00251   u8Status = CLEAR;
00252   u8Index = cu8Protocol * N_ELEMENTS(cau16MirandaReset)/3u;
00253   
00254   
00255   u8Status = u8fnMirandaBatchOp(cu8DSPIInstance, cu8ChipSelect,             \
00256                                 (uint16_t*)&cau16MirandaReset[u8Index],     \
00257                                 (uint16_t*)au16Result,                      \
00258                                 (uint8_t)N_ELEMENTS(au16Result),            \
00259                                 cu8Protocol);
00260   return(u8Status);
00261 }
00262 /*
00263  ******************************************************************************
00264  * u8fnMirandaReadAccelRegisters
00265  ******************************************************************************
00266  */
00267 uint8_t u8fnMirandaReadAccelRegisters(const uint8_t cu8DSPIInstance, \
00268                                       const uint8_t cu8ChipSelect,   \
00269                                       const uint8_t cu8Protocol)
00270 {
00271   uint8_t u8Status;
00272   uint8_t u8Index;
00273   uint16_t au16Result[N_ELEMENTS(cau16MirandaPerformAccelReading)];
00274   
00275   u8Status = CLEAR;
00276   u8Index = cu8Protocol * N_ELEMENTS(cau16MirandaPerformAccelReading)/3;
00277   
00278   u8Status = u8fnMirandaBatchOp(cu8DSPIInstance, cu8ChipSelect,                       \
00279                                 (uint16_t*)&cau16MirandaPerformAccelReading[u8Index], \
00280                                 (uint16_t*)au16Result,                                \
00281                                 (uint8_t)N_ELEMENTS(au16Result),                      \
00282                                 cu8Protocol);
00283   return(u8Status);
00284 }
00285 /*
00286  ******************************************************************************
00287  * u8fnMirandaReadAllRegisters
00288  ******************************************************************************
00289  */
00290 uint8_t u8fnMirandaReadAllRegisters(const uint8_t cu8DSPIInstance, \
00291                                     const uint8_t cu8ChipSelect,   \
00292                                     const uint8_t cu8Protocol)
00293 {
00294   uint8_t u8Status;
00295   uint8_t u8Index;
00296   uint8_t u8Size;
00297   uint16_t au16Result[N_ELEMENTS(cau16MirandaReadAllRegisters)];
00298  
00299   u8Status = CLEAR;
00300   u8Index = cu8Protocol * (1 + MIRANDA_MEMORY_MAP_SIZE/2);
00301   
00302   if(PROTOCOL_2 != cu8Protocol)
00303   {
00304           u8Size = 1 + MIRANDA_MEMORY_MAP_SIZE/2;
00305   }
00306   else
00307   {
00308           /* Number of registers with Protocol 2 + 2 WRITE operations to choose the bank */
00309           u8Size = 30;
00310   }
00311   
00312   u8Status = u8fnMirandaBatchOp(cu8DSPIInstance, cu8ChipSelect,                    \
00313                                 (uint16_t*)&cau16MirandaReadAllRegisters[u8Index], \
00314                                 (uint16_t*)au16Result,                             \
00315                                 (uint8_t)u8Size,                                   \
00316                                 cu8Protocol);
00317   return(u8Status);
00318 }
00319 /*
00320  ******************************************************************************
00321  * u8fnMirandReadAccelSPI
00322  ******************************************************************************
00323  */
00324 uint8_t u8fnMirandaReadAccelSPI(const uint8_t cu8DSPIInstance, \
00325                                 const uint8_t cu8ChipSelect,   \
00326                                 const uint8_t cu8Axis,         \
00327                                 uint16_t* pu16AccelResults)
00328 
00329 {
00330   uint8_t u8Index;
00331   uint8_t u8Status;
00332   uint8_t u8Counter;
00333   uint16_t au16MirandaPerformAccelReading[6];
00334    
00335   u8Status  = CLEAR;
00336   u8Index   = CLEAR;
00337   u8Counter = CLEAR;
00338 
00339     if(MIRANDA_READ_X & cu8Axis)
00340     {
00341         au16MirandaPerformAccelReading[u8Index] = cau16MirandaPerformAccelReadingSPI[0];
00342         au16MirandaPerformAccelReading[++u8Index] = cau16MirandaPerformAccelReadingSPI[1]; 
00343         u8Index++;
00344         u8Counter++;            
00345     } 
00346     if(MIRANDA_READ_Y & cu8Axis)
00347     {
00348         au16MirandaPerformAccelReading[u8Index] = cau16MirandaPerformAccelReadingSPI[2];
00349         au16MirandaPerformAccelReading[++u8Index] = cau16MirandaPerformAccelReadingSPI[3];
00350         u8Index++;              
00351         u8Counter++;
00352     }
00353     if(MIRANDA_READ_Z & cu8Axis)
00354     {
00355         au16MirandaPerformAccelReading[u8Index] = cau16MirandaPerformAccelReadingSPI[4];
00356         au16MirandaPerformAccelReading[++u8Index] = cau16MirandaPerformAccelReadingSPI[5];
00357         u8Counter++;
00358     }
00359     if(CLEAR == (MIRANDA_READ_X & cu8Axis || MIRANDA_READ_Y & cu8Axis || MIRANDA_READ_Z & cu8Axis))
00360     {
00361         /* Request X-axis reading otherwise */
00362         au16MirandaPerformAccelReading[u8Index] = cau16MirandaPerformAccelReadingSPI[0];
00363         au16MirandaPerformAccelReading[++u8Index] = cau16MirandaPerformAccelReadingSPI[1];
00364         u8Counter++;            
00365     }
00366     
00367     u8Status = u8fnMirandaBatchOp(cu8DSPIInstance, cu8ChipSelect,  \
00368                (const uint16_t*)&au16MirandaPerformAccelReading[0],  \
00369                 pu16AccelResults, u8Counter, PROTOCOL_1);
00370     
00371     u8Status |= u8fnMirandaExtractAccelResponse(pu16AccelResults, \
00372                                                 pu16AccelResults, \
00373                                                 u8Counter);
00374   
00375   return(u8Status);
00376 }
00377 /*
00378  ******************************************************************************
00379  * u8fnMirandaBatchOp
00380  ******************************************************************************
00381  */
00382 uint8_t u8fnMirandaBatchOp(const uint8_t cu8DSPIInstance,        \
00383                            const uint8_t cu8ChipSelect,          \
00384                            const uint16_t* pu16DataToSend,       \
00385                            uint16_t* pu16UnfilteredDataReceived, \
00386                            uint8_t u8Size,                       \
00387                            const uint8_t cu8Protocol)
00388 {
00389   uint8_t  u8Status;
00390   uint8_t u8CRC8;
00391   uint16_t u16Counter;
00392   uint16_t u16TimeOut;
00393   uint16_t u16CurrentWordToSend[2u];
00394   uint16_t u16FirstWordReceived;
00395   uint32_t u32WordToCheckParity;
00396   
00397   /* Init locals */
00398   u8Status   = CLEAR;
00399   u16Counter = CLEAR;
00400   u16TimeOut = CLEAR;
00401   u16CurrentWordToSend[0] = CLEAR;
00402   u16CurrentWordToSend[1] = CLEAR;  
00403   u16FirstWordReceived = CLEAR;
00404 
00405   
00406   /* Use DSPI to send config - we need to send one more word since the result*/
00407   /* will be sent with the second one. The content of the last word is       */
00408   /* irrelevant.                                                             */
00409   
00410   /* First, send the first word and simply ignore whatever is received */
00411   /* Check parity if Protocol 0 */
00412   if (PROTOCOL_0 == cu8Protocol)
00413   {
00414           u32WordToCheckParity = (uint32_t)(*pu16DataToSend << 16u) | (uint32_t)(*(pu16DataToSend+1));
00415           u16CurrentWordToSend[0] = u16fnMirandaAddParityToCommand(u32WordToCheckParity);
00416           u16CurrentWordToSend[1] = *(++pu16DataToSend);
00417   }
00418   else if (PROTOCOL_1 == cu8Protocol)
00419   {
00420           u16CurrentWordToSend[0] = *pu16DataToSend;
00421           u16CurrentWordToSend[1] = *(++pu16DataToSend);
00422   }
00423   else if (PROTOCOL_2 == cu8Protocol)
00424   {
00425           u16CurrentWordToSend[0] = *pu16DataToSend;      
00426           u32WordToCheckParity = (uint32_t)(*pu16DataToSend << 16u) | (uint32_t)(*(pu16DataToSend+1));
00427           u8CRC8 = u8fnCRC8((uint8_t*) &u32WordToCheckParity, 0x1D, 24, CLEAR);
00428           u16CurrentWordToSend[1] = *(++pu16DataToSend)| (uint16_t) u8CRC8;                                         
00429   }  
00430   else 
00431   {
00432           u8Status = MIRANDA_ERROR_PROTOCOL;  
00433   }
00434   
00435   if (CLEAR == u8Status)
00436   {
00437   u8Status = u8fnDSPITranscieve(cu8DSPIInstance, cu8ChipSelect,           \
00438                                 (uint16_t*)&u16CurrentWordToSend[0],      \
00439                                 (uint16_t*)&u16FirstWordReceived, 2u);
00440  
00441                                 
00442   /* Next, we shall send the rest of the datagram and store the read-back    */
00443   for(u16Counter = 1u; u16Counter < u8Size; u16Counter++)
00444   {
00445     /* Only proceed if we are clear to go */
00446     if(CLEAR == u8Status)
00447     {
00448       /* Check parity if Protocol 0 */
00449       if (PROTOCOL_0 == cu8Protocol)
00450       {
00451           u32WordToCheckParity = (uint32_t)(*pu16DataToSend << 16u) | (uint32_t)(*(pu16DataToSend+1));
00452           u16CurrentWordToSend[0] = u16fnMirandaAddParityToCommand(u32WordToCheckParity);
00453           u16CurrentWordToSend[1] = *(++pu16DataToSend);
00454       }
00455       else if (PROTOCOL_1 == cu8Protocol)
00456       {
00457           u16CurrentWordToSend[0] = *pu16DataToSend;
00458           u16CurrentWordToSend[1] = *(++pu16DataToSend);
00459       }
00460       else if (PROTOCOL_2 == cu8Protocol)
00461       {
00462           u16CurrentWordToSend[0] = *pu16DataToSend;
00463           u32WordToCheckParity = (uint32_t)(*pu16DataToSend << 16u) | (uint32_t)(*(pu16DataToSend+1));
00464           u8CRC8 = u8fnCRC8((uint8_t*) &u32WordToCheckParity, 0x1D, 24, CLEAR);
00465           u16CurrentWordToSend[1] = *(++pu16DataToSend)| (uint16_t) u8CRC8;             
00466       }  
00467       
00468       /* We must wait for the previous transmission to end before launching  */
00469       /* a new one. We must also make sure we don't stay here forever.       */
00470       u16TimeOut = CLEAR;
00471       while((u8fnIsDSPIBusy(cu8DSPIInstance >> 4u)) && \
00472             (USHRT_MAX > u16TimeOut))
00473       {
00474         u16TimeOut++;
00475       }
00476       
00477       /* If we came out of the above wait without expiring the timer, we     */
00478       /* should continue. otherwise, flag as an error.                       */
00479       if((USHRT_MAX) > u16TimeOut)
00480       {
00481         u8Status = u8fnDSPITranscieve(cu8DSPIInstance, cu8ChipSelect,        \
00482                                       (uint16_t*)&u16CurrentWordToSend[0],   \
00483                                       pu16UnfilteredDataReceived++, 2u);
00484       }
00485       else
00486       {
00487         /* There's something wrong */
00488         u8Status = MIRANDA_TIMED_OUT;
00489       }
00490     }
00491     else
00492     {
00493       /* Loop til we exit */
00494     }
00495   }
00496   
00497   /* Only go on if we still can */
00498   if(CLEAR == u8Status)
00499   {
00500     /* Dummy write just to get the last response */
00501     /* Check parity if Protocol 0 */
00502     if (PROTOCOL_0 == cu8Protocol)
00503     {
00504           u32WordToCheckParity = (uint32_t)(MIRANDA_SN_0 << 16u) | (uint32_t)CLEAR;
00505           u16CurrentWordToSend[0] = u16fnMirandaAddParityToCommand(u32WordToCheckParity);  
00506           u16CurrentWordToSend[1] = (uint16_t)CLEAR;
00507     }
00508     else if (PROTOCOL_1 == cu8Protocol)
00509     {
00510           u16CurrentWordToSend[0] = ((MIRANDA_SN_0 >> 4u) | ((MIRANDA_READ_REGISTER | 0x80u) << CHAR_BIT));
00511           u16CurrentWordToSend[1] = (uint16_t)CLEAR;
00512     }      
00513     else if (PROTOCOL_2 == cu8Protocol)
00514     {
00515           u32WordToCheckParity = (uint32_t)(MIRANDA_BANK_P2 << 16u) | (uint32_t)CLEAR;
00516           u16CurrentWordToSend[0] = MIRANDA_BANK_P2;
00517           u8CRC8 = u8fnCRC8((uint8_t*) &u32WordToCheckParity, 0x1D, 24, CLEAR);
00518           u16CurrentWordToSend[1] = (uint16_t) u8CRC8;          
00519     }           
00520     
00521     /* If we came out of the above wait without expiring the timer, we       */
00522     /* should continue. otherwise, flag as an error.                         */
00523     u16TimeOut = CLEAR;
00524     while((u8fnIsDSPIBusy(cu8DSPIInstance >> 4u)) && \
00525           (USHRT_MAX > u16TimeOut))
00526     {
00527       u16TimeOut++;
00528     }
00529     /* If we came out of the above wait without expiring the timer, we       */
00530     /* should continue. otherwise, flag as an error.                         */
00531     if((USHRT_MAX) > u16TimeOut)
00532     {
00533       u8Status = u8fnDSPITranscieve(cu8DSPIInstance, cu8ChipSelect,      \
00534                                     (uint16_t*)&u16CurrentWordToSend[0], \
00535                                     pu16UnfilteredDataReceived, 2u);
00536     }
00537     else
00538     {
00539       /* There's something wrong */
00540       u8Status = MIRANDA_TIMED_OUT;
00541     }
00542   }
00543   else
00544   {
00545     /* Just get out of here please */
00546   } /* end if (CLEAR == u8Status), the second one */
00547   } /* end if (CLEAR == u8Status), the first one */ 
00548   return(u8Status);
00549 }
00550 /*
00551  ******************************************************************************
00552  * u8fnMMA6800ExtractRegisterResponse
00553  ******************************************************************************
00554  */
00555 uint8_t u8fnMirandaExtractRegisterResponse(const uint16_t* pu16RawResponse, \
00556                                            uint16_t* pu16Response,            \
00557                                            uint8_t u8Size,                  \
00558                                            const uint8_t cu8Protocol)
00559 {
00560   /* Declare local variables */
00561   uint8_t u8Status;
00562   uint8_t u8Counter;
00563   uint32_t u32LocalDataReceived;
00564   
00565   /* Init local variables */
00566   u8Status  = CLEAR;
00567   u8Counter = CLEAR;
00568   
00569   for(; u8Counter < u8Size; u8Counter++)
00570   {
00571 
00572         u32LocalDataReceived = (uint32_t)(*pu16RawResponse << 16) | \
00573                                (uint32_t)(*(pu16RawResponse+1));
00574     u8Status = u8fnMirandaErrorResponseAnalysis(u32LocalDataReceived, cu8Protocol);       
00575     
00576     if (CLEAR == u8Status)
00577     {
00578         if (PROTOCOL_0 == cu8Protocol)
00579         {
00580           /* Data comes in the lower 16-bits according to the spec */
00581           *pu16Response = *(pu16RawResponse+1);
00582         }
00583         else if (PROTOCOL_1 == cu8Protocol)
00584         {
00585           /* Data shifted in the lower 16-bits according to the spec */
00586           *pu16Response = (*pu16RawResponse << 11) | (*(pu16RawResponse+1) >> 5);
00587         }
00588         else if (PROTOCOL_2 == cu8Protocol)
00589         {
00590           /* Data shifted in the lower 16-bits according to the spec */
00591                 *pu16Response = (*pu16RawResponse << 8) | (*(pu16RawResponse+1) >> 8);
00592         }
00593         else
00594         {
00595                 u8Status = MIRANDA_ERROR_PROTOCOL;
00596         }
00597     }
00598     else
00599     {
00600       *pu16Response = CLEAR;
00601     }
00602 
00603     pu16RawResponse++;
00604     pu16Response++;
00605   }
00606   return(u8Status);
00607 }
00608 /*
00609  ******************************************************************************
00610  * u8fnMirandaExtractAccelResponse
00611  ******************************************************************************
00612  */
00613 uint8_t u8fnMirandaExtractAccelResponse(const uint16_t* pu16RawResponse, \
00614                                         uint16_t* pu16Response,          \
00615                                         uint8_t u8Size)
00616 {
00617   /* Declare local variables */
00618   uint8_t u8Status;
00619   uint8_t u8Counter;
00620   uint32_t u32WordToCheckParity;
00621   
00622   /* Init local variables */
00623   u8Status  = CLEAR;
00624   u8Counter = CLEAR;
00625   
00626   for(; u8Counter < u8Size; u8Counter++)
00627   {
00628     /* Check Parity */
00629         u32WordToCheckParity = (uint32_t)(*pu16RawResponse << 16u) | (uint32_t)(*(pu16RawResponse+1));  
00630     if(CLEAR == u32fnCheckOddParity(u32WordToCheckParity, CLEAR))       
00631     {
00632       /* Is this Normal Response */
00633       if(CLEAR != (BIT26 && u32WordToCheckParity))
00634       {
00635        u8Status = CLEAR;
00636        *pu16Response = ((*pu16RawResponse & MIRANDA_RAW_INERTIAL_MASK_UPPER) << 6) |    \
00637                        ((*(pu16RawResponse+1) & MIRANDA_RAW_INERTIAL_MASK_LOWER) >> 10);
00638       }
00639       else
00640       {
00641          /* Identify any errors */
00642          if(MIRANDA_INTERNAL_ERROR == (MIRANDA_EXCEPTION_MASK & *(pu16RawResponse+1)))
00643          {
00644            /* Internal error must be reported */
00645            u8Status = MIRANDA_INTERNAL_ERROR;
00646          }
00647          else if (MIRANDA_SPI_FAULT == (MIRANDA_EXCEPTION_MASK & *(pu16RawResponse+1)))
00648          {
00649            /* SPI transfer fault must be reported */            
00650            u8Status = MIRANDA_SPI_FAULT;        
00651          }
00652          else if (MIRANDA_MISO_COMPARE_FAULT == (MIRANDA_EXCEPTION_MASK & *(pu16RawResponse+1)))
00653          {
00654            /* MISO data compare fault must be reported */       
00655            u8Status = MIRANDA_MISO_COMPARE_FAULT;       
00656          }        
00657          *pu16Response = 0xFFFF;
00658        }        
00659     }
00660     else
00661     {
00662       /* If invalid parity is found, clear this particular response, */
00663       /* and make sure to flag it in the status byte.                */
00664       u8Status |= MIRANDA_INVALID_PARITY;
00665       *pu16Response = 0xFFFE;
00666     }
00667     
00668     pu16RawResponse++;
00669     pu16Response++;
00670   }
00671   return(u8Status);
00672 }
00673 /*
00674  ******************************************************************************
00675  * u8fnMirandaRegisterOperation
00676  ******************************************************************************
00677  */
00678 static uint8_t u8fnMirandaRegisterOperation(const uint8_t u8RegisterOperation,\
00679                                             const uint8_t cu8DSPIInstance,    \
00680                                             const uint8_t cu8ChipSelect,      \
00681                                             uint16_t u16Register,             \
00682                                             const uint16_t u16ValueToWrite,   \
00683                                             uint16_t* pu16DataRead,           \
00684                                             const uint8_t cu8Protocol)
00685 {
00686   uint8_t  u8Status;
00687   uint8_t  u8RegisterAdressStatus;
00688   uint8_t  u8CRC8;
00689   uint16_t u16Counter;
00690   uint16_t au16LocalDataReceived[4u];
00691   uint16_t au16LocalDataToSend[4u];
00692   uint32_t u32WordToCheckParity;  
00693   uint32_t u32LocalDataReceived;
00694   
00695   /* Init locals */
00696   u8Status                  = CLEAR;
00697   u16Counter                = CLEAR;
00698   au16LocalDataReceived[0u] = CLEAR;
00699   au16LocalDataReceived[1u] = CLEAR;
00700   au16LocalDataReceived[2u] = CLEAR;
00701   au16LocalDataReceived[3u] = CLEAR;
00702   
00703   /* Format the MOSI message according to Protocol used */
00704   if (PROTOCOL_0 == cu8Protocol)
00705   {
00706     au16LocalDataToSend[0u]   = u16Register;
00707     au16LocalDataToSend[1u]   = u16ValueToWrite;
00708     /* Second sending to receive MISO response expected */  
00709     au16LocalDataToSend[2u]   = u16Register;
00710     au16LocalDataToSend[3u]   = CLEAR;
00711     /* Mask the register address with the appropriate command */
00712     au16LocalDataToSend[0u] |= (u8RegisterOperation   << CHAR_BIT);
00713     au16LocalDataToSend[2u] |= (MIRANDA_READ_REGISTER << CHAR_BIT);
00714     /* Check/Add parity bit */
00715         u32WordToCheckParity = (uint32_t)(au16LocalDataToSend[0u] << 16u) | \
00716                                (uint32_t)(au16LocalDataToSend[1u]);
00717         au16LocalDataToSend[0u] = u16fnMirandaAddParityToCommand(u32WordToCheckParity);
00718     /* Check/Add parity bit */
00719         u32WordToCheckParity = (uint32_t)(au16LocalDataToSend[2u] << 16u) | \
00720                                (uint32_t)(au16LocalDataToSend[3u]);
00721         au16LocalDataToSend[2u] = u16fnMirandaAddParityToCommand(u32WordToCheckParity);
00722         /* Check if register to handle is within the memory map and register address is even*/
00723         u8RegisterAdressStatus = ((MIRANDA_MEMORY_MAP_SIZE > (u16Register >> 5u)) && \
00724                                           (CLEAR == ((u16Register >> 5u) & BIT0)));
00725   }
00726   else if (PROTOCOL_1 == cu8Protocol)
00727   {
00728     au16LocalDataToSend[0u]   = u16Register;
00729     au16LocalDataToSend[1u]   = u16ValueToWrite;
00730     /* Second sending to receive MISO response expected */  
00731     au16LocalDataToSend[2u]   = u16Register;
00732     au16LocalDataToSend[3u]   = CLEAR;
00733     /* Mask the register address with the appropriate command */
00734     au16LocalDataToSend[0u] |= (u8RegisterOperation   << CHAR_BIT);
00735     au16LocalDataToSend[2u] |= ((MIRANDA_READ_REGISTER | 0x80u) << CHAR_BIT);
00736         /* Check if register to handle is within the memory map and register address is even*/    
00737         u8RegisterAdressStatus = ((MIRANDA_MEMORY_MAP_SIZE > u16Register) && \
00738                                           (CLEAR == (u16Register & BIT0)));
00739   }    
00740   else if (PROTOCOL_2 == cu8Protocol)
00741   {
00742     au16LocalDataToSend[0u]   = u16Register | (u16ValueToWrite >> 8);
00743     au16LocalDataToSend[1u]   = u16ValueToWrite << 8;
00744     /* Second sending to receive MISO response expected */  
00745     au16LocalDataToSend[2u]   = u16Register;
00746     au16LocalDataToSend[3u]   = CLEAR;
00747     /* Mask the register address with the appropriate command */
00748     au16LocalDataToSend[0u] |= (u8RegisterOperation   << CHAR_BIT);
00749     au16LocalDataToSend[2u] |= (MIRANDA_READ_REGISTER << CHAR_BIT);
00750     /* Add CRC word */
00751         u32WordToCheckParity = (uint32_t)(au16LocalDataToSend[0u] << 16u) | \
00752                                (uint32_t)(au16LocalDataToSend[1u]);
00753         u8CRC8 = u8fnCRC8((uint8_t*) &u32WordToCheckParity, 0x1D, 24, CLEAR);
00754         au16LocalDataToSend[1] = au16LocalDataToSend[1] | (uint16_t) u8CRC8;
00755     /* Add CRC word */
00756         u32WordToCheckParity = (uint32_t)(au16LocalDataToSend[2u] << 16u) | \
00757                                (uint32_t)(au16LocalDataToSend[3u]);
00758         u8CRC8 = u8fnCRC8((uint8_t*) &u32WordToCheckParity, 0x1D, 24, CLEAR);
00759         au16LocalDataToSend[3] = au16LocalDataToSend[3] | (uint16_t) u8CRC8;
00760         /* Check if register to handle is within the memory map*/       
00761         u8RegisterAdressStatus = ((MIRANDA_MEMORY_MAP_SIZE_P2 > (u16Register >> 10u)));
00762         
00763   }  
00764   else
00765   {
00766         u8Status = MIRANDA_ERROR_PROTOCOL;
00767   }
00768     
00769   
00770   /* Only continue if the requested read is within the valid memory-map */
00771   if(u8RegisterAdressStatus && (CLEAR == u8Status))
00772   { 
00773     /* Use DSPI to send config - we need to send two words since the result  */
00774     /* will be sent with the second one. The content of the second one is    */
00775     /* irrelevant.                                                           */
00776     u8Status = u8fnDSPITranscieve(cu8DSPIInstance, cu8ChipSelect,        \
00777                                   (uint16_t*)&au16LocalDataToSend,       \
00778                                   (uint16_t*)au16LocalDataReceived, 4u);
00779     if(CLEAR == u8Status)
00780     {
00781       while((DSPI_BUSY_WITH_PREVIOUS_TX ==             \
00782              u8fnIsDSPIBusy(cu8DSPIInstance >> 4u)) && \
00783             ((USHRT_MAX) > u16Counter))
00784       {
00785         /* Wait here for result until we time-out or we get data */
00786         u16Counter++;
00787       } /* End while */
00788       if(USHRT_MAX > u16Counter)
00789       {
00790         /* Check for any flagged errors */
00791         u32LocalDataReceived = (uint32_t)(au16LocalDataReceived[2u] << 16) | \
00792                                (uint32_t)au16LocalDataReceived[3u];
00793         u8Status = u8fnMirandaErrorResponseAnalysis(u32LocalDataReceived, cu8Protocol);
00794       
00795         if(CLEAR == u8Status)
00796         {
00797                 if (PROTOCOL_0 == cu8Protocol)
00798                 {
00799               /* Data comes in the lower 16-bits according to the spec */
00800               *pu16DataRead = au16LocalDataReceived[3u];
00801                 }
00802                 else if (PROTOCOL_1 == cu8Protocol)
00803                 {
00804               /* Data shifted in the lower 16-bits according to the spec */
00805               *pu16DataRead = (uint16_t)(u32LocalDataReceived >> 5);
00806                 }
00807                 else if (PROTOCOL_2 == cu8Protocol)
00808                 {
00809               /* Data shifted in the lower 16-bits according to the spec */
00810               *pu16DataRead = (uint16_t)(u32LocalDataReceived >> 8);
00811                 }
00812                 else
00813                 {
00814                         u8Status = MIRANDA_ERROR_PROTOCOL;
00815                 }
00816         }
00817         else
00818         {
00819           /* Exit with error as marked by u8fnMirandaErrorResponseAnalysis */
00820         }
00821       } /* End if no time-out */
00822       else
00823       {
00824         /* We timed-out. Exit with an error */
00825         u8Status = MIRANDA_TIMED_OUT;
00826       } /* End else - we had a time-out */
00827     } /* End if Status is clear */
00828     else
00829     {
00830       /* We need to exit with an error - already flagged */
00831     }
00832   } /* end if within memory map */
00833   else
00834   {
00835     u8Status = MIRANDA_INVALID_MEMORY_LOCATION;
00836   }
00837   return(u8Status);
00838 }
00839 /*
00840  ******************************************************************************
00841  * u8fnMirandaErrorResponseAnalysis
00842  ******************************************************************************
00843  */
00844 static uint8_t u8fnMirandaErrorResponseAnalysis(const uint32_t cu32ReceivedWord,   \
00845                                                         const uint8_t cu8Protocol)
00846 {
00847   uint8_t  u8Status;
00848   uint16_t cu16ReceivedWordUpper;
00849   uint16_t cu16ReceivedWordLower; 
00850   
00851   u8Status = CLEAR;
00852   
00853   cu16ReceivedWordUpper = (uint16_t) (cu32ReceivedWord >> 16);
00854   cu16ReceivedWordLower = (uint16_t) cu32ReceivedWord;
00855   
00856   if (PROTOCOL_0 == cu8Protocol)
00857   {
00858     /* Once data has been received, check its integrity, starting with     */
00859     /* parity */
00860     if(CLEAR == u32fnCheckOddParity(cu32ReceivedWord, CLEAR))
00861     {
00862       /* Identify any errors */
00863       if(MIRANDA_INTERNAL_ERROR == (MIRANDA_EXCEPTION_MASK & cu16ReceivedWordUpper))
00864       {
00865         /* Internal error must be reported */
00866         u8Status = MIRANDA_INTERNAL_ERROR;
00867       }
00868       else if (MIRANDA_SPI_FAULT == (MIRANDA_EXCEPTION_MASK & cu16ReceivedWordUpper))
00869       {
00870         /* SPI transfer fault must be reported */       
00871         u8Status = MIRANDA_SPI_FAULT;           
00872       }
00873       else if (MIRANDA_MISO_COMPARE_FAULT == (MIRANDA_EXCEPTION_MASK & cu16ReceivedWordUpper))
00874       {
00875         /* MISO data compare fault must be reported */          
00876         u8Status = MIRANDA_MISO_COMPARE_FAULT;          
00877       }
00878       else
00879       {
00880         /* We're good! */
00881       }
00882     }
00883     else
00884     {
00885       u8Status = MIRANDA_INVALID_PARITY;
00886     }
00887   }
00888   else if (PROTOCOL_1 == cu8Protocol)
00889   {
00890             /* Once data has been received, check its integrity, starting with     */
00891             /* parity */
00892             if(CLEAR == u32fnCheckOddParity(cu32ReceivedWord, CLEAR))
00893             {
00894               if (CLEAR == ((BIT30 | BIT29) & cu32ReceivedWord))
00895               {
00896                 /* Identify any errors */
00897                 if(MIRANDA_INTERNAL_ERROR == (MIRANDA_EXCEPTION_MASK & \
00898                                                      (cu16ReceivedWordLower >> 13)))
00899                 {
00900                   /* Internal error must be reported */
00901                   u8Status = MIRANDA_INTERNAL_ERROR;
00902                 }
00903                 else if (MIRANDA_SPI_FAULT == (MIRANDA_EXCEPTION_MASK & \
00904                                                       (cu16ReceivedWordLower >> 13)))
00905                 {
00906                   /* SPI transfer fault must be reported */     
00907                   u8Status = MIRANDA_SPI_FAULT;         
00908                 }
00909                 else if (MIRANDA_MISO_COMPARE_FAULT == (MIRANDA_EXCEPTION_MASK & \
00910                                                                (cu16ReceivedWordLower >> 13)))
00911                 {
00912                   /* MISO data compare fault must be reported */        
00913                   u8Status = MIRANDA_MISO_COMPARE_FAULT;        
00914                 }
00915                 else
00916                 {
00917                   /* We're good! */
00918                 }
00919               }
00920               else
00921               {
00922                   /* We're good! */
00923               }
00924             }
00925             else
00926             {
00927               u8Status = MIRANDA_INVALID_PARITY;
00928             }           
00929   }
00930   else if (PROTOCOL_2 == cu8Protocol)
00931   {
00932             /* Once data has been received, check its integrity, starting with     */
00933             /* CRC */
00934             if(CLEAR == u8fnCRC8((uint8_t*) cu32ReceivedWord, 0x1D, 32, CLEAR))
00935             {
00936               if (CLEAR == ((BIT25 & cu32ReceivedWord) && (BIT24 & cu32ReceivedWord)))
00937               {   
00938                   u8Status = MIRANDA_EXCEPTION;
00939               }
00940               else
00941               {
00942                   /* We're good! */  
00943               }
00944             }
00945             else
00946             {
00947               u8Status = MIRANDA_INVALID_CRC;
00948             }           
00949   }
00950   
00951   return(u8Status);
00952 }
00953 /*
00954  ******************************************************************************
00955  * u16fnMirandaAddParityToCommand
00956  ******************************************************************************
00957  */
00958 static uint16_t u16fnMirandaAddParityToCommand(uint32_t u32RawCommand)
00959 {
00960   uint32_t u32Command;
00961   
00962   u32Command = CLEAR;
00963 
00964   u32Command = u32fnCheckOddParity(u32RawCommand, BIT19);
00965  
00966   return((uint16_t)(u32Command >> BITS_IN_16));
00967 }
00968 /*
00969  ******************************************************************************
00970  *
00971  *  End of file.
00972  *
00973  ******************************************************************************
00974  */